package net.wang.cloud.ticket.service;


import lombok.extern.slf4j.Slf4j;
import net.wang.cloud.dto.OrderDTO;
import net.wang.cloud.ticket.domain.Ticket;
import net.wang.cloud.ticket.repo.TicketRepo;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Slf4j
public class TicketService {

    @Resource
    TicketRepo ticketRepo;

    @Resource
    JmsTemplate jmsTemplate;

    public Ticket save(Ticket ticket) {
        return ticketRepo.save(ticket);
    }

    @Transactional
    @JmsListener(destination = "order:new", containerFactory = "jmsListenerContainerFactory")
    public void handleTicket(OrderDTO orderDTO) {
        log.info("GET ORDER {}", orderDTO);
        long count = ticketRepo.lockTicket(orderDTO.getCustomerId(), orderDTO.getTicketNumber());
        if (count == 1) {
            log.info("LOCKED_SUCCESS");
            orderDTO.setStatus("LOCKED_SUCCESS");
            jmsTemplate.convertAndSend("order:lock", orderDTO);
        } else { //锁票失败，并发时会出现这种情况
            log.info("LOCK_FAIL");
            orderDTO.setStatus("LOCK_FAIL");
            jmsTemplate.convertAndSend("order:fail", orderDTO);
        }
    }

    @Transactional
    @JmsListener(destination = "order:ticket_move", containerFactory = "jmsListenerContainerFactory")
    public void handleTicketMove(OrderDTO orderDTO) {
        log.info("======ticket moving======== order : {}", orderDTO);
       /* Ticket ticket = ticketRepo.findByTicketNumberAndLockUser(orderDTO.getTicketNumber(), orderDTO.getCustomerId());
        ticket.setLockUser(0);
        ticketRepo.save(ticket);*/
        int i = ticketRepo.moveTicket(orderDTO.getCustomerId(), orderDTO.getTicketNumber());
        if (i == 1) {
            orderDTO.setStatus("TICKET_MOVED");
            jmsTemplate.convertAndSend("order:finish", orderDTO);
            log.info("TICKET MOVED !");
        } else {
            log.warn("TICKET ALREADY MOVED !");
        }
    }

    @Transactional
    @JmsListener(destination = "order:ticket_error", containerFactory = "jmsListenerContainerFactory")
    public void handleTicketUnlock(OrderDTO orderDTO) {
        log.info("ticket unlock {}", orderDTO);
        int i = ticketRepo.unLockTicket(orderDTO.getCustomerId(), orderDTO.getTicketNumber());

        if (i == 0) { //解锁失败，原因是已经解锁了
            log.info("TICKET ALREADY UNLOCKED ! orderDTO : {}" + orderDTO);
        } else {
            int count = ticketRepo.unMoveTicket(orderDTO.getCustomerId(), orderDTO.getTicketNumber());
            if(count == 0){
                log.info("TICKET ALREADY UNMOVED OR MOVE FAIL ! orderDTO : {}" + orderDTO);
            }
            orderDTO.setStatus("TICKET_UNLOCKED");
            jmsTemplate.convertAndSend("order:fail", orderDTO);
        }
    }


    @Transactional
    public Ticket lockTicket(OrderDTO orderDTO) {
        log.info("orderDTO = {}", orderDTO);
        Ticket ticket = ticketRepo.findByTicketNumber(orderDTO.getTicketNumber());
        ticket.setLockUser(orderDTO.getCustomerId());
        ticketRepo.save(ticket);
        /* try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            log.error(e.getMessage());
        }*/
        return ticket;

    }


}
